package com.neusoft.zqygbc.neusoft_wenda.adapter;

/**
 * Created by Shinelon on 2017/5/7.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.PhotosEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Jackie on 2017/3/7 16:29
 */

public class GridAdapter extends BaseAdapter{
    private List<PhotosEntity> mList;
    private Context mContext;

    public GridAdapter(Context mContext, List<PhotosEntity> list) {
        this.mContext = mContext;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        PhotosEntity photosEntity = mList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_grid_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        convertView.setVisibility(View.INVISIBLE);
        Picasso.with(mContext).load("http://115.159.93.244/wenda/Public/questionimg/"+photosEntity.getFilename()).into(holder.homequestion_photo);
        int count = 3 - position % 3;
        final TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,
                count,
                Animation.RELATIVE_TO_SELF,
                0,
                Animation.RELATIVE_TO_SELF,
                0,
                Animation.RELATIVE_TO_SELF,
                0);
        translateAnimation.setDuration(count* 100);
//		final Animation animation = AnimationUtils.loadAnimation(parent.getContext(), R.anim.slide_in_right);
        final View finalConvertView = convertView;
        convertView.postDelayed(new Runnable() {
            @Override
            public void run() {
                finalConvertView.startAnimation(translateAnimation);
            }
        }, position * 200);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                finalConvertView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (position == mList.size() - 1) {
                    if (mListener != null) {
                        mListener.onAnimationEnd();
                    }
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView homequestion_photo;

        public ViewHolder(View view) {
            homequestion_photo = (ImageView) view.findViewById(R.id.homequestion_photo);
        }
    }

    public interface OnLastItemAnimationEndListener {
        void onAnimationEnd();
    }

    private OnLastItemAnimationEndListener mListener;

    public void setOnLastItemAnimationEndListener(OnLastItemAnimationEndListener listener) {
        mListener = listener;
    }
}
